package com.pomr.controller.record;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.pomr.model.health.FamilyRecord;
import com.pomr.model.health.FamilyRelation;
import com.pomr.model.health.PersonRecord;
import com.pomr.model.menu.Relation;
import com.pomr.model.sys.User;
import com.pomr.service.record.FamilyService;
import com.pomr.util.common.CommonData;
import com.pomr.util.common.JWT;
import com.pomr.util.common.ResponseData;
import com.pomr.util.page.Page;
import com.pomr.util.page.PageParam;

@Controller
@RequestMapping("/family")
public class FamilyRecordController {
	@Autowired
	@Qualifier("familyServiceImpl")
	private FamilyService familyService;
	
	@RequestMapping(value="/inter-save",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData saveFamilyBefore(HttpServletRequest request){
		ResponseData pd = null;
		try {
			pd = ResponseData.ok();
			Map<String, Object> commonmap = CommonData.data;
			pd.putDataValue("Relation", commonmap.get("Relation"));
		} catch (Exception e) {
			e.printStackTrace();
			pd = ResponseData.exceptionError();
		}
		return pd;
	}
	
	@RequestMapping(value="/inter-savefam",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData saveFamily(HttpServletRequest request,@RequestBody FamilyRecord record){
		ResponseData pd = null;
		try {
			User user = JWT.findUserByToken(request);
			record.setUser(user);
			if(familyService.saveFamily(record)){
				pd = ResponseData.ok();
			}else{
				pd = ResponseData.fail();
			}
		} catch (Exception e) {
			e.printStackTrace();
			pd = ResponseData.exceptionError();
		}
		return pd;
	}
	
	
	@RequestMapping(value="/inter-searchbefore",method=RequestMethod.POST)
	@ResponseBody
	public Page<List<FamilyRecord>> searchFamilyBefore(HttpServletRequest request,@RequestBody Map<String,String> map){
		Page<List<FamilyRecord>> p = null;
		try {
			p = new Page<List<FamilyRecord>>();
			String tj = map.get("name");
			String page = map.get("page").toString();//页码
			String pagesize = map.get("limit").toString();//每页条数
			p.setCunrrentPage(Integer.valueOf(page));
			p.setPageSize(Integer.valueOf(pagesize));
			p = familyService.searchFamilys(p, tj);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return p;
	}
	
	@RequestMapping(value="/inter-searchFamInfo",method=RequestMethod.GET)
	@ResponseBody
	public Page<List<FamilyRecord>> searchFamilyInfo(HttpServletRequest request,PageParam param){
		Page<List<FamilyRecord>> p = null;
		try {
			p = new Page<List<FamilyRecord>>();
			String page = param.getPage();//页码
			String pagesize = param.getLimit();//每页条数
			Map<String, String> map = param.getParams();
			String tj = map.get("id");
			p.setCunrrentPage(Integer.valueOf(page));
			p.setPageSize(Integer.valueOf(pagesize));
			p = familyService.searchFamilyInfo(p, tj);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return p;
	}
	
	/**
	 * 查询家庭成员信息
	 * @param request
	 * @param param
	 * @return
	 */
	@RequestMapping(value="/inter-searchFamPerInfo",method=RequestMethod.GET)
	@ResponseBody
	public Page<List<FamilyRecord>> searchFamilyOfPersonInfo(HttpServletRequest request,PageParam param){
		Page<List<FamilyRecord>> p = null;
		try {
			p = new Page<List<FamilyRecord>>();
			String page = param.getPage();//页码
			String pagesize = param.getLimit();//每页条数
			Map<String, String> map = param.getParams();
			String tj = map.get("id");
			p.setCunrrentPage(Integer.valueOf(page));
			p.setPageSize(Integer.valueOf(pagesize));
			p = familyService.searchFamilyOfPersonInfo(p, tj);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return p;
	}
	
	/**
	 * 保存家庭成员
	 * @param request
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/inter-saverelation",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData saveFamilyPerson(HttpServletRequest request,@RequestBody FamilyRelation relation){
		ResponseData pd = null;
		try{
			if(familyService.saveRelation(relation)){
				pd = ResponseData.ok();
			}
		}catch (Exception e) {
			e.printStackTrace();
			pd = ResponseData.exceptionError();
		}
		return pd;
	}
	
	@RequestMapping(value="/inter-deletelist",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData deleteFamily(HttpServletRequest request,@RequestBody Map<String,String> map){
		ResponseData pd = null;
		try{
			String[] ids = map.get("id").toString().split(",");
			List<String> idsl = Arrays.asList(ids);
			if(familyService.deleteFamilys(idsl)){
				pd = ResponseData.ok();
			}else{
				pd = ResponseData.fail();
			}
		}catch (Exception e) {
			e.printStackTrace();
			pd = ResponseData.exceptionError();
		}
		return pd;
	}
	
	@RequestMapping(value="/inter-deleterelations",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData deleteRelations(HttpServletRequest request,@RequestBody Map<String,String> map){
		ResponseData pd = null;
		try{
			String[] ids = map.get("id").toString().split(",");
			List<String> idsl = Arrays.asList(ids);
			if(familyService.deleteRelations(idsl)){
				pd = ResponseData.ok();
			}else{
				pd = ResponseData.fail();
			}
		}catch (Exception e) {
			e.printStackTrace();
			pd = ResponseData.exceptionError();
		}
		return pd;
	}
	
	@RequestMapping(value="/inter-switchrelation",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData switchRelation(HttpServletRequest request,@RequestBody Map<String,String> map){
		ResponseData pd = null;
		try{
			String id = map.get("id");//成员关系id
			String fid = map.get("fid");//家庭档案id
			if(familyService.switchRelation(id,fid)){
				pd = ResponseData.ok();
			}else{
				pd = ResponseData.fail();
			}
		}catch (Exception e) {
			e.printStackTrace();
			pd = ResponseData.exceptionError();
		}
		return pd;
	}
	
	@RequestMapping(value="/inter-searchinfo",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData editFamily(HttpServletRequest request,@RequestBody FamilyRecord record){
		ResponseData pd = null;
		try{
			record = familyService.searchRecords(record);
			if(record == null){
				pd = ResponseData.fail();
			}else{
				pd = ResponseData.ok();
				pd.putDataValue("record", record);
				Map<String, Object> commonmap = CommonData.data;
				pd.putDataValue("Relation", commonmap.get("Relation"));
			}
			
		}catch (Exception e) {
			e.printStackTrace();
			pd = ResponseData.exceptionError();
		}
		return pd;
	}
	
	@RequestMapping(value="/inter-updateinfo",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData updateFamilyInfo(HttpServletRequest request,@RequestBody FamilyRecord record){
		ResponseData pd = null;
		try{
			if(familyService.updateFamilyInfo(record)){
				pd = ResponseData.ok();
			}else{
				pd = ResponseData.fail();
			}
		}catch (Exception e) {
			e.printStackTrace();
			pd = ResponseData.exceptionError();
		}
		return pd;
	}
	
	
	@RequestMapping(value="/inter-update",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData updateFamily(HttpServletRequest request,@RequestBody Map<String,Object> map){
		ResponseData pd = null;
		System.out.println("------------更新家庭信息");
		
		try {
			FamilyRecord fr = new FamilyRecord();
			if(map.get("id")==null || map.get("id").toString().equals("")){
				pd = ResponseData.custom("无法获取主键");
			}
			fr.setId(Integer.valueOf(map.get("id").toString()));
			fr.setAddr(map.get("addr")==null?"":map.get("addr").toString());
			fr.setTel(map.get("tel")==null?"":map.get("tel").toString());
			Map<String, Map<String, String>> rs = (Map<String, Map<String,String>>) map.get("familyRelations");
			FamilyRelation fre = null;
			Relation realtion = null;
			PersonRecord record = null;
			Set<FamilyRelation> familyRelations = new HashSet<FamilyRelation>();
			for(Entry<String, Map<String, String>> r:rs.entrySet()){
				fre = new FamilyRelation();
				realtion = new Relation();
				realtion.setId(Integer.valueOf(r.getValue().get("relid")));
				fre.setRelation(realtion);
				record = new PersonRecord();
				record.setId(Integer.valueOf(r.getValue().get("perid")));
				fre.setPersonrecord(record);
				familyRelations.add(fre);
			}
			fr.setFamilyRelations(familyRelations);
			
			if(familyService.updateFamily(fr)){
				pd = ResponseData.ok();
			}else{
				pd = ResponseData.fail();
			}
		} catch (Exception e) {
			e.printStackTrace();
			pd = ResponseData.exceptionError();
		}
		return pd;
	}
}
